package com.iot08.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.iot08.api.controller.base.BaseController;
import com.iot08.api.service.SchoolTimetableService;
import com.iot08.api.service.supplement.KafkaService;
import com.iot08.api.vo.request.SchoolTimeTableUpdateReqVo;
import com.iot08.api.vo.request.base.BaseAccountReqVO;
import com.iot08.common.constant.ConstKafka;
import com.iot08.common.entity.app.SchoolTimetable;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 课程表
 * @author weichao 1299101858@qq.com
 * @version 1.0
 * @date 4/28/22 3:18 PM
 */
@Slf4j
@RestController
@RequestMapping("/getway/schooltimetable/")
public class SchoolTimeTableController extends BaseController {

    @Resource
    private KafkaService kafkaService;

    @Resource
    private SchoolTimetableService schoolTimetableService;


    @GetMapping("/{imei}")
    public ResponseMap getSchoolTimeTable (@Validated BaseAccountReqVO vo, @PathVariable("imei") String imei){
        SchoolTimetable schoolTimetable = schoolTimetableService.findById(imei);
        if (schoolTimetable == null) {

            schoolTimetable = new SchoolTimetable();
            schoolTimetable.setId(imei);
            schoolTimetable.setOperatorId(vo.getOpenid());
            schoolTimetableService.add(schoolTimetable);
        }
        JSONObject res = new JSONObject();
        res.put("classTimesInfo", schoolTimetable.getClassTimesInfo());
        res.put("coursesInfoDict", schoolTimetable.getCoursesInfoDict());
        res.put("afternoonBreakTime", schoolTimetable.getAfternoonBreakTime());
        res.put("morningBreakTime", schoolTimetable.getMorningBreakTime());
        return ResponseMap.success(res);
    }

    @PostMapping("/{imei}")
    public ResponseMap postSchoolTimeTable (@Validated @RequestBody SchoolTimeTableUpdateReqVo vo, @PathVariable("imei") String imei){
        SchoolTimetable schoolTimetable = schoolTimetableService.findById(imei);
        schoolTimetable.setOperatorId(vo.getOpenid());
        if (vo.getMorningBreakTime() != null) {
            schoolTimetable.setMorningBreakTime(vo.getMorningBreakTime());
        }

        if (vo.getAfternoonBreakTime() != null) {
            schoolTimetable.setAfternoonBreakTime(vo.getAfternoonBreakTime());
        }

        if (vo.getClassTimesInfo() != null) {
            schoolTimetable.setClassTimesInfo(new JSONObject(vo.getClassTimesInfo()));
        }

        if (vo.getCoursesInfoDict() != null) {
            schoolTimetable.setCoursesInfoDict(new JSONObject(vo.getCoursesInfoDict()));
        }
        schoolTimetableService.update(schoolTimetable);

        JSONObject pushDevMsg = new JSONObject();
        pushDevMsg.put("classTimesInfo", schoolTimetable.getClassTimesInfo());
        pushDevMsg.put("coursesInfoDict", schoolTimetable.getCoursesInfoDict());
        pushDevMsg.put("afternoonBreakTime", schoolTimetable.getAfternoonBreakTime());
        pushDevMsg.put("morningBreakTime", schoolTimetable.getMorningBreakTime());
        pushDevMsg.put("type", ConstKafka.DEVICE_TYPE_downSchoolTimeTable);
        kafkaService.emitDeviceDemand(imei, ConstKafka.DEVICE_TYPE_downSchoolTimeTable, pushDevMsg, vo.getOpenid());
        return ResponseMap.success();
    }


}
